package com.cpoopc.fuckapp.utils.security;


import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import android.util.Log;

/**
 * 提供DES加密支持
 * <p>
 * Description:描述
 * </p>
 */
public class DES {

    private static final String TAG = "DES";

    /**
     * 对输入进行DES加密
     * @param key 用于生成DES加密所需的秘钥
     * @param info 被加密文本，如果为null则返回null
     * @return 加密后的文本，如果为null说明输入为null
     * @throws SecurityException 加密失败时抛出异常,具体异常通过getMessage查看
     */
    public static byte[] encrypt(String key, String info) throws SecurityException {
        if (key == null) {
            throw new IllegalArgumentException(SecurityUtil.ERROR_KEY_IS_NULL);
        }
        if (null == info || info.equals("")) {
            return null;
        }
        SecretKey secretKey = createDESKey(key);
        Cipher c1 = null;
        try {
            c1 = Cipher.getInstance("DES");
            c1.init(Cipher.ENCRYPT_MODE, secretKey);
            return c1.doFinal(info.getBytes());
        } catch (NoSuchAlgorithmException e) {
            Log.w(TAG, "" + e.getMessage());
            throw new SecurityException("NoSuchAlgorithmException");
        } catch (NoSuchPaddingException e) {
            Log.w(TAG, "" + e.getMessage());
            throw new SecurityException("NoSuchPaddingException");
        } catch (InvalidKeyException e1) {
            e1.printStackTrace();
            throw new SecurityException("InvalidKeyException");
        } catch (IllegalBlockSizeException e) {
            Log.w(TAG, "" + e.getMessage());
            throw new SecurityException("IllegalBlockSizeException");
        } catch (BadPaddingException e) {
            Log.w(TAG, "" + e.getMessage());
            throw new SecurityException("BadPaddingException");
        }
    }

    /**	
     * 对输入进行DES加密
     * @param key 用于生成DES加密所需的秘钥
     * @param info 被加密文本，如果为null则返回null
     * @return 加密后的文本，如果为null说明输入为null
     * @throws SecurityException 加密失败时抛出异常,具体异常通过getMessage查看
     */
    public static byte[] encrypt(String key, byte[] info) throws SecurityException {
        return encrypt(key, SecurityUtil.toHexString(info));
    }

    /**	
     * 对输入进行DES解密
     * @param key 用于生成DES解密所需的秘钥
     * @param info 被解密文本，如果为null则返回null
     * @return 解密后的文本，如果为null说明输入为null
     * @throws SecurityException  加密失败时抛出异常,具体异常通过getMessage查看
     */
    public static byte[] decrypt(String key, byte[] info) throws SecurityException {
        if (key == null) {
            throw new IllegalArgumentException(SecurityUtil.ERROR_KEY_IS_NULL);
        }
        if (null == info || info.equals("")) {
            return null;
        }
        SecretKey secretKey = createDESKey(key);
        Cipher c1;
        try {
            c1 = Cipher.getInstance("DES");
            c1.init(Cipher.DECRYPT_MODE, secretKey);
            return c1.doFinal(info);
        } catch (NoSuchAlgorithmException e) {
            Log.w(TAG, "" + e.getMessage());
            throw new SecurityException("NoSuchAlgorithmException");
        } catch (NoSuchPaddingException e) {
            Log.w(TAG, "" + e.getMessage());
            throw new SecurityException("NoSuchPaddingException");
        } catch (InvalidKeyException e) {
            Log.w(TAG, "" + e.getMessage());
            throw new SecurityException("InvalidKeyException");
        } catch (IllegalBlockSizeException e) {
            Log.w(TAG, "" + e.getMessage());
            throw new SecurityException("IllegalBlockSizeException");
        } catch (BadPaddingException e) {
            Log.w(TAG, "" + e.getMessage());
            throw new SecurityException("BadPaddingException");
        }
    }

    /**	
     * 对输入进行DES解密
     * @param key 用于生成DES解密所需的秘钥
     * @param info 被解密文本，如果为null则返回null
     * @return 解密后的文本，如果为null说明输入为null
     * @throws SecurityException  加密失败时抛出异常,具体异常通过getMessage查看
     */
    public static byte[] decrypt(String key, String info) throws SecurityException {
        return decrypt(key, SecurityUtil.convertHexString(info));
    }

    /**	
     * 生成DES加密所需的秘钥
     * @param keys 用于生成DES加密/解密所需的秘钥
     * @return 生成的秘钥
     */
    private static SecretKey createDESKey(String keys) {
        if (null == keys || keys.equals("")) {
            return null;
        }
        SecretKey deskey = new SecretKeySpec(keys.getBytes(), "DES");
        return deskey;
    }

}
